package net.zjitc.homework.hw2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author Administrator
 * @Date 2020/5/21 14:48
 **/
@WebServlet("/forwardAndRedirect2")
public class ForwardAndRedirect2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /* 显示内容：欢迎你，username
        * 1、 通过响应response对象： 获取输出流，通过输出流写数据
        * 2、通过域对象，获取保存在域对象中的内容 */
//        Object user = req.getAttribute("user");

        Object user = req.getSession().getAttribute("user");

        /*响应，中文乱码：*/
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        pw.write("欢迎你，"+user);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
